﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Management;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            //Retrieve CPU and memory information from WMI classes
            int numsockets = 0;
            try
            {
                //Search Win32Processor class
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Processor");

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    numsockets++;
                    txtlogCPU.Text = txtlogCPU.Text + queryObj["NumberOfLogicalProcessors"].ToString();
                    txtCPU.Text = txtCPU.Text + queryObj["NumberOfCores"].ToString();
                    txtManufacturer.Text = txtManufacturer.Text + queryObj["Manufacturer"].ToString();
                    txtModel.Text = txtModel.Text + queryObj["Name"].ToString();
                    txtDescription.Text = txtDescription.Text + queryObj["Description"].ToString();
                    txtLoad.Text = txtLoad.Text + queryObj["LoadPercentage"].ToString();
                }
                searcher.Dispose();
            }
            catch (ManagementException e)
            {
                txtDescription.Text = "An error occurred while querying for WMI data: " + e.Message.ToString() ;
            }
            txtSockets.Text = numsockets.ToString();
        }

        
        //All threads are disabled at the beginning
        public bool th1Enabled = false;
        public bool th2Enabled = false;
        public bool th3Enabled = false;
        public bool th4Enabled = false;
        public bool th5Enabled = false;
        public bool th6Enabled = false;
        public bool th7Enabled = false;
        public bool th8Enabled = false;
        public bool th9Enabled = false;
        public bool th10Enabled = false;
        public bool th11Enabled = false;
        public bool th12Enabled = false;
        public bool th13Enabled = false;
        public bool th14Enabled = false;
        public bool th15Enabled = false;
        public bool th16Enabled = false;
        public bool th17Enabled = false;
        public bool th18Enabled = false;
        public bool th19Enabled = false;
        public bool th20Enabled = false;
        public bool th21Enabled = false;
        public bool th22Enabled = false;
        public bool th23Enabled = false;
        public bool th24Enabled = false;
        public bool th25Enabled = false;
        public bool th26Enabled = false;
        public bool th27Enabled = false;
        public bool th28Enabled = false;
        public bool th29Enabled = false;
        public bool th30Enabled = false;
        public bool th31Enabled = false;
        public bool th32Enabled = false;
        public bool th33Enabled = false;
        public bool th34Enabled = false;
        public bool th35Enabled = false;
        public bool th36Enabled = false;
        public bool th37Enabled = false;
        public bool th38Enabled = false;
        public bool th39Enabled = false;
        public bool th40Enabled = false;

        // create thread start delegate instances - contains the method to execute by the threads
        public ThreadStart ts1 = new ThreadStart(run1);

        // create new threads
        public static Thread thrd1;
        public static Thread thrd2;
        public static Thread thrd3;
        public static Thread thrd4;
        public static Thread thrd5;
        public static Thread thrd6;
        public static Thread thrd7;
        public static Thread thrd8;
        public static Thread thrd9;
        public static Thread thrd10;
        public static Thread thrd11;
        public static Thread thrd12;
        public static Thread thrd13;
        public static Thread thrd14;
        public static Thread thrd15;
        public static Thread thrd16;
        public static Thread thrd17;
        public static Thread thrd18;
        public static Thread thrd19;
        public static Thread thrd20;
        public static Thread thrd21;
        public static Thread thrd22;
        public static Thread thrd23;
        public static Thread thrd24;
        public static Thread thrd25;
        public static Thread thrd26;
        public static Thread thrd27;
        public static Thread thrd28;
        public static Thread thrd29;
        public static Thread thrd30;
        public static Thread thrd31;
        public static Thread thrd32;
        public static Thread thrd33;
        public static Thread thrd34;
        public static Thread thrd35;
        public static Thread thrd36;
        public static Thread thrd37;
        public static Thread thrd38;
        public static Thread thrd39;
        public static Thread thrd40;

        //MODIFICACION
        //Creates an array with the thread variables
        public Thread[] listaThread = new Thread[40] { thrd1, thrd2, thrd3, thrd4, thrd5, thrd6, thrd7, thrd8, thrd9,
        thrd10, thrd11, thrd12, thrd13, thrd14, thrd15, thrd16, thrd17, thrd18, thrd19, thrd20, thrd21, thrd22, thrd23,
        thrd24, thrd25, thrd26, thrd27, thrd28, thrd29, thrd30, thrd31, thrd32, thrd33, thrd34, thrd35, thrd36, thrd37,
        thrd38, thrd39, thrd40};

        //Method executed by the threads
        //
        // It is an infinite loop that will use all the available CPU time
        static void run1()
        {
            long i = 0;
            while (true)
            {
                i++;
                if (i > 10000000)
                {
                    i = 0;
                }
            }

        }
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void refreshCPUInfoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (timer1.Enabled == true) 
                {
                    timer1.Enabled = false;
                } else
                {
                    timer1.Enabled = true;
                }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            
            //Update CPU load information
            try
            {
                //CPU load string
                string CPUload = "";

                //Search Win32Processor class
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Processor");

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    CPUload =  CPUload + " / " + queryObj["LoadPercentage"].ToString();
                    
                }
                txtLoad.Text = CPUload;
                searcher.Dispose();
            }
            catch (ManagementException e1)
            {
                Console.WriteLine("An error occurred while querying for WMI data: " + e1.Message);
            }
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            //Start the desired number of CPU threads
            if ((int)(numThreads.Value) > 0)
            {
                for (int j = 0; j < (int)(numThreads.Value); j++)
                {
                    if (listaThread[j] == null)
                    {
                        listaThread[j] = new Thread(ts1);
                        listaThread[j].IsBackground = true;
                        // start thread
                        listaThread[j].Start();
                    }
                    else if (listaThread[j].ThreadState == ThreadState.Aborted | listaThread[j].ThreadState == ThreadState.Stopped)
                    {
                        listaThread[j] = new Thread(ts1);
                        listaThread[j].IsBackground = true;
                        // start thread
                        listaThread[j].Start();
                    }
                    
                }

                for (int k = (int) (numThreads.Value); k < 40; k++)
                {
                    if (listaThread[k] != null)
                    {
                        if (listaThread[k].IsAlive)
                        {
                            listaThread[k].Abort();
                        }
                    }
                    
                }

                
            }
            else
            {
                for (int l = 0; l < 40; l++)
                {
                    if (listaThread[l] != null)
                    {
                        if (listaThread[l].IsAlive)
                        {
                            listaThread[l].Abort();
                        }
                    }
                    
                }
                
            }
            lblRunning.Text = numThreads.Value.ToString();
        }


    }

}

